﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Timers;



namespace WebControlApp.UI
{
    class Model
    {
        private UI.Presenter M_Presenter;
        private TcpClient Client;
        private Stream TcpStream;
        public Timer timer1;
        public Timer SyncTimer;
        private byte[] mPacketData;
        private byte[] mReturnData;
        private bool mSensorOverride;
        private bool mCalibrate;
        private bool mAutoBrake;

        public Uri VideoAddress { get; set; }
        public string currentVideoConnection { get; set; }
        public string currentCommandConnection { get; set; }
        public string currentCommandPort { get; set; }
        public string RawSensorData { get; set; }
        public string LogView { get; set; }
        public double LeftIRSensor { get; set; }
        public double RightIRSensor { get; set; }
        public double UltraSonicSensor { get; set; }
        public double LeftMotorCurrent { get; set; }
        public double RightMotorCurrent { get; set; }
        public int LeftEncoder { get; set; }
        public int RightEncoder { get; set; }
        public int MaxSpeed { get; set; }
        public int TimerLength { get; set; }
        public bool SensorOverride
        {
            get { return mSensorOverride; }
            set
            {
                mSensorOverride = value;
                if (SensorOverride == true)
                {
                    PacketData[8] |= 0x80;
                }
                if (SensorOverride == false)
                {
                    PacketData[8] &= 0x7f;
                }
            }
        }
        public bool Calibrate{
            get { return mCalibrate; } 
            set
            {
                mCalibrate = value;
                if (Calibrate == true)
                {
                    PacketData[8] |= 0x01;
                }
                if (Calibrate == false)
                {
                    PacketData[8] &= 0xfe;
                }
            }
        }
        public bool AutoBrake
        {
            get { return mAutoBrake; }
            set
            {
                mAutoBrake = value;
                if (AutoBrake == true)
                {
                    PacketData[8] &= 0xf7;
                }
                if (AutoBrake == false)
                {
                    PacketData[8] |= 0x08;
                }
            }
        }

 
        public Model(UI.Presenter p)
        {
            M_Presenter = p;
            Client = new TcpClient();

            //CHANGE DEFAULT IP ADDRESS HERE
            currentCommandConnection = "139.78.68.84";
            currentVideoConnection = "http://139.78.68.84:8080/javascript_simple.html";
            currentCommandPort = "31337";

            //SET PACKET SIZES HERE
            mPacketData = new byte[9];
            mReturnData = new byte[15];

            PacketData[0] = 86;
            PacketData[1] = 65;
            PacketData[4] = 43;
            PacketData[5] = 43;
            PacketData[6] = 43;
            PacketData[7] = 43;
            PacketData[8] = 0;
        }

        public byte[] ReturnData
        {
            get
            {
                return mReturnData;
            }
        }

        public byte[] PacketData
        {
            get { return mPacketData; }
        }

        public void SendData()
        {
            WebControlApp.Logic.sendData(TcpStream,mPacketData,mReturnData);
        }

        public void Connect()
        {
            WebControlApp.Logic.Connect(Client,currentCommandConnection,currentCommandPort,currentVideoConnection,TcpStream);
        }

        public void Dissconnect()
        {
            WebControlApp.Logic.Disconnect(TcpStream,Client);
        }

        public void SyncTick()
        {
            PacketData[2] = 36;
            PacketData[3] = 36;
            SendData();
        }

    }
}
